package com.zhny.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhny.dao.MenuDao;

import com.zhny.pojo.BUser;
import com.zhny.pojo.Menu;

import com.zhny.service.MenuService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired //自动注入bean
    private MenuDao menuDao;

    @Override
    public Page<Menu> pageQuery(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(queryString);
        return (Page<Menu>)menuDao.selectByExample(example);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return  menuDao.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param menu
     */
    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
    }

    /**
     * 编辑
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        menuDao.updateByPrimaryKey(menu);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        menuDao.deleteByPrimaryKey(id);
    }


    /**
     * 构建查询对象
     * @param queryString
     * @return
     */
    private Example createExample(String queryString){
        Example example=new Example(BUser.class);
        Example.Criteria criteria = example.createCriteria();
        if(queryString!=null){
            String[] s = queryString.split("_");
            if (s[0].equals("role")) {
                criteria.andLike("role", "%" + s[1] + "%");
            } else if (s[0].equals("info")) {
                criteria.andLike("info", "%" + s[1] + "%");
            }
        }
        return example;
    }

}
